package com.ruoyi.system.service.util;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.enums.IsSystemEnum;
import com.ruoyi.common.core.enums.UserStatus;
import com.ruoyi.common.core.utils.http.HttpUtils;
import com.ruoyi.system.api.domain.SysDept;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.SysPost;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 接口调用封装
 *
 * @author lcy
 */
public class CallUtils {

    //登录第三方并获得token
    public static String login(String username, String password,String url) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("username",username);
        jsonObject.put("password",password);
        jsonObject.put("loginType",0);
        String s = HttpUtils.dataPost(url, jsonObject, null);
        JSONObject j = JSONObject.parseObject(s);
        return j.getJSONObject("data").getString("access_token");
    }

    //获取部门数据
    public static List<SysDept> getDept(String accessToken,String url) {
        //获取部门数据
        String s1 = HttpUtils.dataPost(url, new JSONObject(),accessToken);
        // 解析整个 JSON 字符串
        JSONObject j1 = JSONObject.parseObject(s1);
        // 获取 data 字段对应的 JSONArray
        JSONArray dataArray = j1.getJSONArray("data");
        List<SysDept> sysDepts = new ArrayList<>();
        for (int i = 0; i < dataArray.size(); i++) {
            SysDept sysDept = new SysDept();
            JSONObject dataObject = dataArray.getJSONObject(i);
            // 提取对象中的字段信息
            String deptId = dataObject.getString("deptId");
            //赋值部门ID
            sysDept.setDeptId(deptId);
            //所属类型
            sysDept.setDeptType(dataObject.getString("deptType"));
            //父ID
            sysDept.setParentId(dataObject.getString("parentId"));
            //部门名
            sysDept.setDeptName(dataObject.getString("deptName"));
            //排序值
            sysDept.setOrderNum(Integer.parseInt(dataObject.getString("orderNum")));
            //负责人
            sysDept.setLeader(dataObject.getString("leader"));
            //联系电话
            sysDept.setPhone(dataObject.getString("phone"));
            //启禁用状态
            sysDept.setStatus(dataObject.getString("status"));
            //删除状态
            sysDept.setDelFlag(dataObject.getString("isDel"));
            //是否自建
            sysDept.setIsSys(IsSystemEnum.NO.getVal());
            sysDepts.add(sysDept);
        }
        return sysDepts;
    }



    //获取用户数据
    public static List<SysUser> getUser(String accessToken, String url, List<String> deptIds) throws ParseException {
        Set<SysUser> users = new HashSet<>();
        Map<String, String> statusMap = new HashMap<>();
        Map<String, String> phoneMap = new HashMap<>();
        //根据部门ID获取人员信息
        for (String deptId : deptIds) {
            String param = "deptId="+deptId;
            String s2 = HttpUtils.sendGet(url,param, Constants.UTF8,accessToken);
            // 解析整个 JSON 字符串
            JSONObject j2 = JSONObject.parseObject(s2);
            // 获取 data 字段对应的 JSONArray
            JSONArray dataArray2 = j2.getJSONArray("data");
            for (int i = 0; i < dataArray2.size(); i++) {
                SysUser sysUser = new SysUser();
                //岗位集合
                Set<SysPost> sysPosts = new HashSet<>();
                JSONObject object = dataArray2.getJSONObject(i);

                //提取对象中字段信息
                sysUser.setNickName(object.getString("nickname"));
                sysUser.setSex(object.getString("sex"));
                //获取用户唯一标识
                sysUser.setUserIdSync(object.getString("userId"));
                //工号
                sysUser.setUserName(object.getString("employee"));
                //手机号
                sysUser.setPhonenumber(object.getString("phoneNumber"));
                //岗位
                //sysUser.setPostCode(object.getString("userType"));
                //班组
                //是否为组长
                sysUser.setDeptId(object.getString("deptId"));
                //创建人
                sysUser.setCreateBy(object.getString("createBy"));
                // 定义原始时间字符串的格式
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
                // 设置时区为东八区 (GMT+08:00)
                dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
                //创建时间
                sysUser.setCreateTime(object.getString("createAt")!=null?dateFormat.parse(object.getString("createAt")):null);
                //更新人
                sysUser.setUpdateBy(object.getString("updateBy"));
                //更新时间
                sysUser.setUpdateTime(object.getString("updateAt")!=null?dateFormat.parse(object.getString("updateAt")):null);
                //删除标志
                //boolean flag = object.getString("isDel").equals("0")&&object.getString("status").equals("0");
                sysUser.setDelFlag(object.getString("isDel").equals(UserStatus.OK.getCode())?object.getString("isDel"):UserStatus.DELETED.getCode());
                //是否系统自建
                sysUser.setIsSys(IsSystemEnum.NO.getVal());
                //获取所属公司
                sysUser.setTenantId(object.getString("tenantId"));
                users.add(sysUser);
            }
        }
        return new ArrayList<>(users.stream().filter(o->(!(o.getUserName().equals("")))&&(o.getUserName()!=null)).collect(Collectors.toSet()));
    }



}
